package com.openthinks.joyniproject.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.openthinks.joyniproject.dao.BaseDao;
import com.openthinks.joyniproject.dao.DeptDao;
import com.openthinks.joyniproject.model.Dept;
import com.openthinks.joyniproject.utils.BaseUtil;
import com.openthinks.joyniproject.utils.SQLConfigLoader;

/**
 * 部门 Dao 实现
 * @since 
 *		v1.0
 * @version
 * 		v1.0, 2013-06-27 19:20:10
 * @author 
 *		Cloud
 */
@Repository
public class DeptDaoImpl implements DeptDao {

	@Autowired
	private BaseDao baseDao;
	
	@Override
	public List<Dept> findAll() {
		List<Dept> list = new ArrayList<Dept>();
		String sql = SQLConfigLoader.getSQL("Dept.findAll");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Map<String, Object>> rets = baseDao.queryForList(sql, paramMap);
		for (Map<String, Object> ret : rets) {
			list.add(getDept(ret));
		}
		return list;
	}

	@Override
	public Dept findById(Long id) {
		String sql = SQLConfigLoader.getSQL("Dept.findById");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", id);
		Map<String, Object> ret = baseDao.queryForMap(sql, paramMap);
		return getDept(ret);
	}
	
	@Override
	public List<Dept> findSubByPid(Long pid) {
		List<Dept> list = new ArrayList<Dept>();
		String sql = SQLConfigLoader.getSQL("Dept.findSubByPid");
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("pid", pid);
		List<Map<String, Object>> rets = baseDao.queryForList(sql, paramMap);
		for (Map<String, Object> ret : rets) {
			list.add(getDept(ret));
		}
		return list;
	}
	
	private Dept getDept(Map<String, Object> map) {
		Dept dept = new Dept();
		dept.setId(BaseUtil.parseLong(BaseUtil.parseString(map.get("id"))));
		dept.setName(BaseUtil.parseString(map.get("name")));
		return dept;
	}

}
